package cloud.maque.auth.gitee;

import cloud.maque.auth.annotations.ThirdLoginAnn;
import cloud.maque.auth.conf.AuthProperties;
import cloud.maque.auth.model.Oauth2AccessTokenRes;
import cloud.maque.auth.model.Oauth2GetAccesstokenReq;
import cloud.maque.auth.model.Oauth2User;
import cloud.maque.auth.service.Oauth2CallBackService;
import cloud.maque.common.core.enums.LoginType;
import cn.hutool.core.util.URLUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
@ThirdLoginAnn(value = LoginType.GITEE)
public class Oauth2GiteeCallBackServiceImpl implements Oauth2CallBackService {

    @Autowired
    AuthProperties authProperties;

    /**
     * 获取AccessToken
     *
     * @param code
     * @return
     */
    @Override
    public Oauth2AccessTokenRes getAccessToken(String code) {
        String url = "https://gitee.com/oauth/token?grant_type=authorization_code&code=" + code + "&client_id=" + authProperties.getGitee().getClientId() + "&redirect_uri=" + URLUtil.encode(authProperties.getGitee().getRedirectUri()) + "&client_secret=" + authProperties.getGitee().getClientSecret();

        Oauth2GetAccesstokenReq accesstokenReq = new Oauth2GetAccesstokenReq();
        //accesstokenReq.setClientId(authProperties.getGitee().getClientId());
        //accesstokenReq.setClientSecret(authProperties.getGitee().getClientSecret());

        return Oauth2CallBackService.super.getAccessToken(accesstokenReq, url);
    }

    /**
     * 获取第三方的用户信息
     * <p>
     * <p>
     * {"id":88519,"login":"4color","name":"四嗮","avatar_url":"https://gitee.com/assets/no_portrait.png","url":"https://gitee.com/api/v5/users/4color","html_url":"https://gitee.com/4color","remark":"","followers_url":"https://gitee.com/api/v5/users/4color/followers","following_url":"https://gitee.com/api/v5/users/4color/following_url{/other_user}","gists_url":"https://gitee.com/api/v5/users/4color/gists{/gist_id}","starred_url":"https://gitee.com/api/v5/users/4color/starred{/owner}{/repo}","subscriptions_url":"https://gitee.com/api/v5/users/4color/subscriptions","organizations_url":"https://gitee.com/api/v5/users/4color/orgs","repos_url":"https://gitee.com/api/v5/users/4color/repos","events_url":"https://gitee.com/api/v5/users/4color/events{/privacy}","received_events_url":"https://gitee.com/api/v5/users/4color/received_events","type":"User","blog":null,"weibo":null,"bio":"","public_repos":12,"public_gists":0,"followers":0,"following":4,"stared":32,"watched":57,"created_at":"2014-04-23T08:49:38+08:00","updated_at":"2024-12-11T09:13:02+08:00","email":null}
     *
     * @param accessToken
     * @return
     */
    @Override
    public Oauth2User getUser(String accessToken) {
        String url = "https://gitee.com/api/v5/user?access_token=" + accessToken;
        HashMap map = Oauth2CallBackService.super.getUserBase(url,null);

        Oauth2User user = new Oauth2User();
        user.setUserId(map.get("id").toString());
        user.setUserName(map.get("login").toString());
        user.setNickName(map.get("name").toString());
        return user;
    }

    /**
     * 刷新token
     *
     * @param refreshToken
     * @return
     */
    @Override
    public String refreshToken(String refreshToken) {
        return "";
    }
}
